<template>
  <div class="container order-container">
    <i-tab-bar i-class="order-tab" :current="active" color="#ee7559" @change="changeTab">
      <i-tab-bar-item key="0" img="../../../static/images/icon-o-qb.png" current-img="../../../static/images/icon-o-qb-active.png"
        title="全部"></i-tab-bar-item>
      <i-tab-bar-item key="1" img="../../../static/images/icon-o-dfk.png" current-img="../../../static/images/icon-o-dfk-active.png"
        title="待付款"></i-tab-bar-item>
      <i-tab-bar-item key="2" img="../../../static/images/icon-o-dfh.png" current-img="../../../static/images/icon-o-dfh-active.png"
        title="待发货"></i-tab-bar-item>
      <i-tab-bar-item key="3" img="../../../static/images/icon-o-dsh.png" current-img="../../../static/images/icon-o-dsh-active.png"
        title="待收货"></i-tab-bar-item>
      <i-tab-bar-item key="4" img="../../../static/images/icon-o-dpj.png" current-img="../../../static/images/icon-o-dpj-active.png"
        title="待评价"></i-tab-bar-item>
    </i-tab-bar>
    <div class="receipt-list order-list" v-if="orderList">
      <div class="order" v-for="(order,index) in orderList" :key="index" @click="$router.push({ path: '/pages/orderInfo/orderInfo',query:{order_id:order.order_id}})">
        <div class="order-title">
          <div class="receipt-store-header">
            <img src="../../../static/images/store-icon.png" alt="">
            <span class="store-name">{{order.store_name}}</span>
          </div>
          <!-- <span class="order-sn">订单编号：{{order.order_sn}}</span> -->
          <span class="order-status" v-if="order.status==0">待付款</span>
          <span class="order-status" v-if="order.status==1">待发货</span>
          <span class="order-status" v-if="order.status==2">待收货</span>
          <span class="order-status" v-if="order.status==3">退货中</span>
          <span class="order-status" v-if="order.status==4">退货完成</span>
          <span class="order-status" v-if="order.status==5">待评价</span>
          <span class="order-status" v-if="order.status==6">已评价</span>
        </div>
        <div class="receipt-box">
          <receipt-module v-for="(item,goodIndex) in order.goods_list" :key="goodIndex" :data="item" :allChecked="checked"
            :showChecked="false" :note="item.remark?item.remark:''" :change="receiptChange"></receipt-module>
        </div>
        <div class="order-controller">
          <div class="line"></div>
          <i-button v-if="order.status==0||order.status==1" type="default" size="small" i-class="order-btn default-btn"
            shape="circle" @click.stop="handleOrder(1,order.order_id)">取消订单</i-button>
          <i-button v-if="order.status==0" type="primary" size="small" i-class="order-btn primary-btn" shape="circle"
            @click.stop="selectPayType(order)">去付款</i-button>
          <!-- <i-button v-if="order.status==2||order.status==5" type="default" size="small" i-class="order-btn default-btn"
            shape="circle">查看物流</i-button> -->
          <!-- <i-button v-if="order.status==2" type="default" size="small" i-class="order-btn default-btn" shape="circle"
            @click.stop="handleOrder(3,order.order_id)">退货</i-button> -->
          <i-button v-if="order.status==2" type="primary" size="small" i-class="order-btn primary-btn" shape="circle"
            @click.stop="handleOrder(2,order.order_id)">确认收货</i-button>
          <i-button v-if="order.status==5" type="primary" size="small" i-class="order-btn primary-btn" @click.stop="evaluation(order)"
            shape="circle">去评价</i-button>
        </div>
      </div>
      <i-load-more v-if="!orderList&&isLoadEnd" :tip="'暂无数据'" :loading="loading" />
      <i-load-more v-if="!isLoadEnd" :tip="'加载更多数据'" :loading="loading" />
      <i-load-more v-if="orderList&&isLoadEnd" :tip="'没有更多数据了'" :loading="loading" />
    </div>
    <van-dialog id="van-dialog" />
    <i-toast id="toast" />
    <i-modal title="请选择支付方式" :visible="visible" :actions="actions" action-mode="vertical" @iclick="pay"></i-modal>
  </div>
</template>

<script>
  import {
    mapState,
    mapMutations
  } from "Vuex";
  // 依赖
  import Dialog from "@/../static/vant/dialog/dialog";
  const {
    $Toast
  } = require("@/../static/iview/base/index");
  import receiptModule from "@/components/receiptModule";
  import request from "@/api/request";
  import {
    wxPay
  } from '@/utils/index'
  export default {
    components: {
      receiptModule
    },
    data() {
      return {
        isLoadEnd: false,
        active: 0,
        page: 1,
        loading: false,
        visible: false,
        order_sn: null,
        order_id: null,
        actions: [{
            name: "余额支付",
            color: "#EE7559",
            pay_type: 1
          },
          {
            name: "微信支付",
            color: "#404040",
            pay_type: 2
          },
          // {
          //   name: "转账",
          //   color: "#404040",
          //   pay_type: 3
          // },
          {
            name: "取消",
            color: "#EE7559"
          }
        ]
      };
    },
    computed: {
      ...mapState(["orderList", "pay"])
    },
    methods: {
      ...mapMutations(["setOrderList", "setUserInfo", "setPay", 'setEvaluationList']),
      evaluation(data) {
        this.setEvaluationList(data)
        this.$router.push({
          path: '/pages/evaluation/evaluation'
        })
      },
      pay(e) {
        this.visible = false;
        // 1余额支付 2-微信支付 3-转账
        let title = "";
        let message = "";
        switch (e.mp.detail.index) {
         case 0:
            title = "余额支付";
            message = "将使用平台余额支付货款！";
            break;
          case 1:
            title = "微信支付";
            message = "将使用微信支付支付货款！";
            break;
          default:
            return;
        }
        
        Dialog.confirm({
            title,
            message
          })
          .then(() => {
            wxPay({
              pay_type: this.actions[e.mp.detail.index].pay_type,
              order_no: this.order_sn
            }).then(res => {
              request.getUserInfo().then(res => {
                console.log(res)
                this.page = 1
                this.setOrderList([])
                this.setUserInfo(res.data);
                this.getOrderList();
              });
            })
          })
          .catch(() => {
            console.log("取消");
            // on cancel
          });
      },
      handleOrder(type, order_id) {
        // 1 - 取消订单 2 - 确认收货 3 - 退货
        let title = "";
        let message = "";
        switch (type) {
          case 1:
            title = "取消订单";
            message = "确定取消订单？";
            break;
          case 2:
            title = "确认收货";
            message = "确认货品无误后，请确认收货!";
            break;
          default:
            return;
        }
        Dialog.confirm({
            title,
            message
          })
          .then(() => {
            console.log(11111);
            // on confirm
            request
              .handleOrder({
                type,
                order_id
              })
              .then(res => {
                $Toast({
                  content: res.message,
                  type: res.code == 0 ? "success" : "warning"
                });
                request.getUserInfo().then(res => {
                  console.log(res)
                  this.page = 1
                  this.setOrderList([])
                  this.setUserInfo(res.data);
                  this.getOrderList();
                });
              }).catch(res => {
                console.log(res)
                $Toast({
                  content: res.message,
                  type: res.code == 0 ? "success" : "warning"
                });
              })
          })
          .catch(() => {
            console.log("取消");
            // on cancel
          });
      },
      selectPayType(order) {
        this.visible = true;
        this.order_sn = order.order_sn;
        this.order_id = order.order_id;
      },
      getOrderList() {
        // 获取订单列表
        this.loading = true;
        request
          .getOrderList({
            status: this.active,
            page: this.page,
          })
          .then(res => {
            let time = setTimeout(() => {
              clearTimeout(time);
              if (res.data.length < 20) {
                this.isLoadEnd = true
              }
              let list = []
              if (this.orderList) {
                list = this.orderList.concat(res.data)
              } else {
                list = res.data
              }
              console.log(list)
              this.setOrderList(list);
              this.loading = false;
            }, 300);
          });
      },
      getStoreOrderList() {
        // 获取订单列表
        this.loading = true;
        request
          .getStoreOrderList({
            status: this.active,
            page: this.page,
          })
          .then(res => {
            let time = setTimeout(() => {
              clearTimeout(time);
              if (res.data.length < 20) {
                this.isLoadEnd = true
              }
              let list = []
              if (this.orderList) {
                list = this.orderList.concat(res.data)
              } else {
                list = res.data
              }
              this.setOrderList(res.data);
              this.loading = false;
            }, 300);
          });
      },
      changeTab(e) {
        this.page = 1
        this.isLoadEnd = false
        this.active = e.mp.detail.key;
        switch (this.type) {
          case 'order':
            this.setOrderList([]);
            this.getOrderList();
            break;
          case 'store':
            this.setOrderList([]);
            this.getStoreOrderList();
            break;
        }
      }
    },
    onReachBottom() {
      this.page++;
      if (this.$route.query.type && this.$route.query.type == 'store') {
        this.type = this.$route.query.type
        this.getStoreOrderList()
      } else {
        this.type = 'order'
        this.getOrderList();
      }
    },
    mounted() {

      if (this.$route.query.status) {
        this.active = this.$route.query.status;
      }
      if (this.$route.query.type && this.$route.query.type == 'store') {
        this.type = this.$route.query.type
        this.getStoreOrderList()
      } else {
        this.type = 'order'
        this.getOrderList();
      }
      switch (this.type) {
        case 'order':
          this.setOrderList([]);
          this.getOrderList();
          break;
        case 'store':
          this.setOrderList([]);
          this.getStoreOrderList();
          break;
      }
    }
  };

</script>

<style lang="less">
  .container {
    flex: 1;
    display: flex;
    flex-direction: column;
  }

  .order-container {
    display: flex;
    flex-direction: column;
    overflow: hidden;

    .order-tabs {}
  }

  .order-list {
    padding: 15px 15px;
    flex: 1;
    overflow: auto;
  }

  .order {
    margin-bottom: 15px;
    overflow: hidden;
  }

  .order-title {
    display: flex;
    justify-content: space-between;
    position: relative;
    background: #fff;
    padding: 0 15px;

    &:after {
      content: "";
      position: absolute;
      top: 0;
      left: 15px;
      width: 200%;
      height: 200%;
      transform: scale(0.5);
      transform-origin: 0 0;
      pointer-events: none;
      box-sizing: border-box;
      border: 0 solid #e9eaec;
      border-bottom-width: 1px;
    }

    .order-sn {
      line-height: 34px;
      font-size: 12px;
      color: #606060;
    }

    .order-status {
      line-height: 34px;
      font-size: 12px;
      color: #ee7559;
    }
  }

  .order-controller {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    background: #fff;
    position: relative;

    .line {
      position: absolute;
      top: 0;
      left: 15px;
      right: 15px;

      &:after {
        content: "";
        position: absolute;
        top: 0;
        left: 0;
        width: 200%;
        height: 200%;
        transform: scale(0.5);
        transform-origin: 0 0;
        pointer-events: none;
        box-sizing: border-box;
        border: 0 solid #f8f8f8;
        border-top-width: 1px;
      }
    }

    .order-btn {
      font-size: 12px;
      height: 24px;
      line-height: 24px;
      margin: 8px 15px 8px 0;
      background: none !important;
    }

    .default-btn {
      color: #a0a0a0 !important;
      box-shadow: inset 0 0 0 1px #c9c9c9 !important;
    }

    .primary-btn {
      color: #ee7559 !important;
      box-shadow: inset 0 0 0 1px #ee7559 !important;
    }
  }

  .receipt-box {
    padding-bottom: 15px;
    background: #fff;
  }

</style>
